<template>
  <div class="page_root" :key="queryUpdateFlag" data-page-name="分批进展" data-page-identification="z_9hv5ffqrjg">
    <a-spin :spinning="pageLoading.loading" :tip="pageLoading.tip">
      <!-- begin vid_e5uo9pnmse 自定义模板_76chq4apmm -->
      <div class="body">
        <div class="top-search">
          <div class="border-div">
            <a-row :gutter="24">
              <a-col :span="6">
                <a-form-item label="批次">
                  <a-select
                    allow-clear
                    show-search
                    v-model:value="state.filter.batch"
                    :options="state.batchList"
                    placeholder="请选择"
                    :field-names="{ label: 'name', value: 'name' }"
                    :filterOption="
                                            (input, option) => {
                                                return option.name.toLowerCase().indexOf(input.toLowerCase()) >= 0
                                            }
                                        "
                    @change="getData"
                  />
                </a-form-item>
              </a-col>
              <a-col :span="2">
                <a-button type="primary" @click="resetData">重置</a-button>
              </a-col>
            </a-row>
          </div>
        </div>
        <div class="content">
          <div class="tip">
            <div class="one">
              <span class="start">
                第一批 <span class="title-color-font-big">{{ state.batch1Data.length }}</span
                >家
              </span>
              <span class="title-color-font">{{ state.batch1Data.batch1Check }}</span
              >家进入验收阶段，<span class="title-color-font">{{ state.batch1Data.batch1Verify }}</span
              >家进入验证阶段，<span class="title-color-font">{{ state.batch1Data.batch1Run }}</span
              >家进入正式运行阶段
            </div>
            <div class="two">
              <span class="start">
                第二批 <span class="title-color-font-big"> {{ state.batch2Data.length }}</span
                >家
              </span>
              <span class="title-color-font">{{ state.batch2Data.batch2Check }}</span
              >家进入验收阶段，<span class="title-color-font">{{ state.batch2Data.batch2Verify }}</span
              >家进入验证阶段，<span class="title-color-font">{{ state.batch2Data.batch2Run }} </span>家进入正式运行阶段，<span class="title-color-font">{{
                                state.batch2Data.batch2TestRun
              }}</span
              >家进入试运行阶段，<span class="title-color-font">{{
                                state.batch2Data.batch2CollectionMapping
              }}</span
              >家进入数据积累阶段
            </div>
          </div>
          <a-table
            bordered
            row-key="batch"
            :columns="columns"
            :data-source="state.dataSource"
            :row-class-name="
                            (_record, index) => (index % 2 === 1 ? 'table-striped-even' : 'table-striped-odd')
                        "
            :scroll="{ y: 540 }"
            :loading="state.tableLoading"
            :pagination="false"
          />
        </div>
      </div>
      <!-- end vid_e5uo9pnmse 自定义模板_76chq4apmm -->
    </a-spin>
  </div>
</template>
<script setup lang="tsx">
  import {
      h, ref, reactive, toRaw, markRaw, watch, computed,
      onBeforeMount, onMounted, onBeforeUnmount, onUnmounted, onActivated, onDeactivated
    } from "vue";
    import { useRoute, useRouter } from 'vue-router';
    const router = useRouter();
    const route = useRoute();
    import { get, post, put, del, request, requestWithHeaders } from '@/utils/http/Axios';
    import { cloneDeep, localGet, merge, getDifferenceArray, traversalTreeStructure, checkCaseCondition, renderStringInTemplate, handleBreadcrumbJump, throttle, permission, getComponentParamsValue } from '@/utils/index';
    import { isNullOrEmpty, isEmptyArr, isNullOrUnDef, isContained, isArray, isEmpty, isObject } from '@/utils/is';
    import dayjs from 'dayjs';
    import { getPagePcChartsDataSourceConvert } from '@/utils/chart';
    import { message, notification, Modal } from 'ant-design-vue'
    import { propertyFormatByNumber, propertyFormatByPercentage, propertyFormatByDate, parseSubFromDataList } from '@/utils/format';
    import colorTheme from '@/design/custom-theme.json';
    
    import UserTableRecords from '@/model/UserTableRecords';
    import { getAppBaseUrl } from '@/utils/index';
    import { sendToken } from "@/hooks/useIframeToken";
    import { useSystemStore } from "@/store/modules/system";
    const systemStore = useSystemStore();
    



      // 获取传入的流程ID和组件参数
      const props = defineProps({
            'processInstanceId': {
            type: [String,Number,Boolean],
            default: undefined,
        },
      'projectId': {
            type: [Number,String],
            default: undefined,
        },
      })
      const immediateWatch = (source, callback) => {
        watch(source, callback, {deep: true,immediate: true })
      }



  /* -------------------------- 属性定义 -------------------------- */

      const setTimeOutList = [];
      const pageLoading = reactive({
        loading: false,
        tip: undefined
      });
      const authInfo = localGet('app-user-store');




      // 处理校验
      const setSubFormName =(parentInfo, name)=>{
        return [parentInfo.parents, parentInfo.cycleIndex, name];
      }


  /* ========================== 属性定义 ========================== */


  /* -------------------------- 元素数据引用 -------------------------- */


  /* ========================== 元素数据引用 ========================== */


  /* -------------------------- 页面数据 -------------------------- */


  /* ========================== 页面数据 ========================== */



  /* -------------------------- 响应 -------------------------- */

  /* ========================== 响应 ========================== */


  /* -------------------------- 函数 -------------------------- */

  /* ========================== 函数 ========================== */


  /* -------------------------- 页面参数 -------------------------- */

    const pageParameter_state = reactive({
        'processInstanceId': props.processInstanceId ?? route.query.processInstanceId,
    })

  /* ========================== 页面参数 ========================== */


  /* -------------------------- 组件事件处理方法 -------------------------- */

    // ---------------------------- 组件数据同步 ----------------------------

    // ============================ 组件数据同步 ============================


  /* ========================== 组件事件处理方法 ========================== */


  /* -------------------------- 生命周期 -------------------------- */
  // 用于参数变更后重新执行自定义指令
      const queryUpdateFlag = ref(0);
      onMounted(() => { ;

    });

    onUnmounted(() => {
      // 销毁定时器
      setTimeOutList.forEach(({ repeat, timer }) => {
        repeat ?  clearInterval(timer): clearTimeout(timer);
      });
    });

  /* ========================== 生命周期 ========================== */


  /* -------------------------- 数据关联处理 -------------------------- */





  /* -------------------------- 表单数据同步 -------------------------- */
  /* ========================== 表单数据同步 ========================== */

  /* ========================== 数据关联处理 ========================== */


  /* -------------------------- 不同组件自定义的处理逻辑 -------------------------- */

  /* ========================== 不同组件自定义的处理逻辑 ========================== */


  // ================================定制化逻辑========================================
   onMounted(() => {
          getBatchList()
          getData()
      })
      const state = reactive({
          filter: {
              batch: undefined
          },
          tableLoading: false,
          batchList: [],
          dataSource: [],
          batch1Data: {},
          batch2Data: {}
      })
      // 区域
      function getBatchList() {
          get(`/solution-ppm/api/v2/built-in-system-ppm/dashboard_config/batch_priority_time/里程碑进度表/批次`).then(
              data => {
                  if (data.code !== 200) {
                      message.error(data.message)
                      return
                  }
                  state.batchList = data.data
              }
          )
      }
      let isCalculated = false; // 标志变量，防止重复计算
      function getData() {
          state.tableLoading = true
          post('/solution-ppm/api/v2/built-in-system-ppm/project_progress/batch_progress/search', state.filter)
              .then(data => {
                  if (data.code !== 200) {
                      return
                      state.dataSource = []
                  }
                  state.dataSource = data.data
                  if (!isCalculated) {
                      isCalculated = true;
                      getNumber();
                  }
              })
              .finally(() => {
                  state.tableLoading = false
              })
      }

      function getNumber() {
          // 处理第一批数据
          state.batch1Data = state.dataSource.find(item => item.batch === '第一批')?.children || []
          state.batch1Data.batch1Check = state.batch1Data.reduce((acc, item) => acc + (item.check || 0), 0)
          state.batch1Data.batch1Verify = state.batch1Data.reduce((acc, item) => acc + (item.verify || 0), 0)
          state.batch1Data.batch1Run = state.batch1Data.reduce((acc, item) => acc + (item.run || 0), 0)
          state.batch1Data.batch1TestRun = state.batch1Data.reduce((acc, item) => acc + (item.testRun || 0), 0)
          state.batch1Data.batch1CollectionMapping = state.batch1Data.reduce(
              (acc, item) => acc + (item.collectionMapping || 0),
              0
          )

          // 处理第二批数据
          state.batch2Data = state.dataSource.find(item => item.batch === '第二批')?.children || []
          state.batch2Data.batch2Check =
              state.batch2Data.reduce((acc, item) => acc + (item.check || 0), 0) +
              (state.dataSource.find(item => item.batch === '2')?.check || 0)
          state.batch2Data.batch2Verify =
              state.batch2Data.reduce((acc, item) => acc + (item.verify || 0), 0) +
              (state.dataSource.find(item => item.batch === '2')?.verify || 0)
          state.batch2Data.batch2Run =
              state.batch2Data.reduce((acc, item) => acc + (item.run || 0), 0) +
              (state.dataSource.find(item => item.batch === '2')?.run || 0)
          state.batch2Data.batch2TestRun =
              state.batch2Data.reduce((acc, item) => acc + (item.testRun || 0), 0) +
              (state.dataSource.find(item => item.batch === '2')?.testRun || 0)
          state.batch2Data.batch2CollectionMapping =
              state.batch2Data.reduce((acc, item) => acc + (item.collectionMapping || 0), 0) +
              (state.dataSource.find(item => item.batch === '2')?.collectionMapping || 0)
      }
      function resetData() {
          state.filter.batch = undefined
          getData()
      }
      const columns = ref([
          {
              title: '批次',
              align: 'center',
              dataIndex: 'batch',
              customCell: prams => {
                  console.log(prams)
                  return {
                      style: {
                          fontWeight: prams.children ? '600' : '400'
                      }
                  }
              }
          },
          {
              title: '数据积累',
              align: 'center',
              dataIndex: 'collectionMapping'
          },
          {
              title: '自学习表数据确认',
              align: 'center',
              dataIndex: 'processingTableConfirm'
          },
          {
              title: '数据建模',
              align: 'center',
              dataIndex: 'model'
          },
          {
              title: '试运行',
              align: 'center',
              dataIndex: 'testRun'
          },
          {
              title: '正式运行',
              align: 'center',
              dataIndex: 'run'
          },
          {
              title: '验证',
              align: 'center',
              dataIndex: 'verify'
          },
          {
              title: '验收',
              align: 'center',
              dataIndex: 'check'
          },
          {
              title: '验收完成',
              align: 'center',
              dataIndex: 'checkFinish'
          },
          {
              title: '合计',
              align: 'center',
              dataIndex: 'total'
          }
      ])
</script>
<style lang="less" scoped>
  .page_root {
  width: 100%;
  height: 100%;
  overflow: auto;
  }
  .page_root :deep(.@{ant-prefix}-spin-nested-loading),
  .page_root :deep(.@{ant-prefix}-spin-container)
   {
      height: 100%;
  }
  .page_root :deep(.@{ant-prefix}-spin-container .@{ant-prefix}-table-wrapper)
   {
      display: flex;
      flex-flow: column nowrap;
      justify-content: flex-start;
  }
  .page_root :deep(.@{ant-prefix}-table)
   {
    flex: auto;
    overflow: auto;
  }
  .table-moreBtn_down {
    overflow: hidden; white-space: nowrap; text-overflow: ellipsis; width: 100px;
  }

  .table-moreBtn_menu :deep(.table-moreBtn_menu-item):hover {
    background: #ebf1ff;
  }

  /*  表格树结构样式处理，展开按钮层级增加*/
  :deep(.@{ant-prefix}-table-row-expand-icon) {
    z-index: 1;
  }
  /*  表格树结构 由于每个表格项采用自定义组件，使用绝对定位布局，开启树结构导致显示异常，暂时使用层级定位位置 */
  :deep(.@{ant-prefix}-table-row-level-1 .@{ant-prefix}-table-cell-with-append .cell-wrapper-left) {
    left: 15px !important;
    width: calc(100% - 15px);
  }
  :deep(.@{ant-prefix}-table-row-level-2 .@{ant-prefix}-table-cell-with-append .cell-wrapper-left) {
    left: 30px !important;
    width: calc(100% - 30px);
  }
  :deep(.@{ant-prefix}-table-row-level-3 .@{ant-prefix}-table-cell-with-append .cell-wrapper-left) {
    left: 45px !important;
    width: calc(100% - 45px);
  }
  :deep(.@{ant-prefix}-table-row-level-4 .@{ant-prefix}-table-cell-with-append .cell-wrapper-left) {
    left: 60px !important;
    width: calc(100% - 60px);
  }

  /* 穿梭框改变默认样式，为根据组件样式控制左右栏宽度。 */
  :deep(.@{ant-prefix}-transfer-list)
   {
      height: 100%;
      width: 50%;
  }


  /*
     分批进展
     */
      .page_root {
          overflow: hidden;
          height: 100%;
      }
      .body {
          height: 100%;
      }
      .top-search {
          background-color: #fff;
          padding: 24px;
      }
      .top-search :deep(.ant3-row) {
          height: 32px;
      }
      .top-search :deep(.ant3-col) {
          height: 32px;
      }
      .content {
          padding: 0 24px;
          height: 100%;
          background-color: #fff;
      }
      .content :deep(.@{ant-prefix}-table-thead) > tr > th {
          background: #3875ff;
          color: #fff;
          font-weight: 600;
          padding: 8px 16px;
          border-right: 1px solid #d9d9d9 !important;
          position: relative;
      }
      .content :deep(.@{ant-prefix}-table-tbody) tr > td {
          font-size: 14px;
          font-family: arial, helvetica, 'microsoft yahei';
          font-weight: 400;
          color: #262626;
          border-bottom: 1px solid #d9d9d9;
          padding: 8px 16px;
          word-break: break-all;
      }
      .tip {
          height: 70px;
          padding: 9px 16px;
          background: #ebf1ff;
          border: 1px solid #3875ff;
          border-radius: 4px;
          font-size: 14px;
          color: #262626;
          letter-spacing: 0;
          line-height: 22px;
          font-weight: 400;
          margin-bottom: 16px;
      }
      .title-color-font,
      .title-color-font-big {
          font-size: 16px;
          color: #3875ff;
          letter-spacing: 0;
          line-height: 22px;
          font-weight: 600;
      }
      .title-color-font-big {
          font-size: 16px;
      }
      .border-div {
          padding-bottom: 16px;
          border-bottom: 1px solid #d9d9d9;
      }
      .start {
          color: #262626;
          display: inline-block;
          margin-right: 9px;
          font-weight: 600;
      }
      .content :deep(.table-striped-even) td {
          background-color: #f8fafc;
      }
      /*
      分批进展
      */
</style>